package api

import (
	"context"
	"errors"
	"fmt"
	"gogomcp-server/internal/home"
	"net/http"

	"github.com/mark3labs/mcp-go/mcp"
	"github.com/mark3labs/mcp-go/server"

	"github.com/gin-gonic/gin"
)

func Home(c *gin.Context) {
	home.Home()

	c.String(http.StatusOK, "OK")
}

// Mcp 处理基本的 HTTP MCP 请求
func Mcp(c *gin.Context) {
	// 创建 MCP 服务器
	s := server.NewMCPServer(
		"GoGoMCP Server",
		"1.0.0",
		server.WithResourceCapabilities(true, true),
		server.WithLogging(),
	)

	// 添加计算器工具
	calculatorTool := mcp.NewTool("calculate",
		mcp.WithDescription("执行基本算术运算"),
		mcp.WithString("operation",
			mcp.Required(),
			mcp.Description("要执行的运算 (add, subtract, multiply, divide)"),
			mcp.Enum("add", "subtract", "multiply", "divide"),
		),
		mcp.WithNumber("x",
			mcp.Required(),
			mcp.Description("第一个数字"),
		),
		mcp.WithNumber("y",
			mcp.Required(),
			mcp.Description("第二个数字"),
		),
	)

	// 添加计算器处理器
	s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
		op := request.Params.Arguments["operation"].(string)
		x := request.Params.Arguments["x"].(float64)
		y := request.Params.Arguments["y"].(float64)

		var result float64
		switch op {
		case "add":
			result = x + y
		case "subtract":
			result = x - y
		case "multiply":
			result = x * y
		case "divide":
			if y == 0 {
				return nil, errors.New("不能除以零")
			}
			result = x / y
		}

		return mcp.NewToolResultText(fmt.Sprintf("%.2f", result)), nil
	})

	// Start the server
	if err := server.ServeStdio(s); err != nil {
		c.String(http.StatusInternalServerError, "Error starting server: %v", err)
	}
	c.String(http.StatusOK, "MCP server started")
}

// McpSSE 处理 SSE 请求
func McpSSE(c *gin.Context) {
	// 创建一个新的 MCPServer 实例
	mcpServer := server.NewMCPServer(
		"gin-mcp-server",
		"1.0.0",
		server.WithResourceCapabilities(true, true),
		server.WithLogging(),
	)

	// 添加计算器工具
	calculatorTool := mcp.NewTool("calculate",
		mcp.WithDescription("执行基本算术运算"),
		mcp.WithString("operation",
			mcp.Required(),
			mcp.Description("要执行的运算 (add, subtract, multiply, divide)"),
			mcp.Enum("add", "subtract", "multiply", "divide"),
		),
		mcp.WithNumber("x",
			mcp.Required(),
			mcp.Description("第一个数字"),
		),
		mcp.WithNumber("y",
			mcp.Required(),
			mcp.Description("第二个数字"),
		),
	)

	// 添加计算器处理器
	mcpServer.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
		op := request.Params.Arguments["operation"].(string)
		x := request.Params.Arguments["x"].(float64)
		y := request.Params.Arguments["y"].(float64)

		var result float64
		switch op {
		case "add":
			result = x + y
		case "subtract":
			result = x - y
		case "multiply":
			result = x * y
		case "divide":
			if y == 0 {
				return nil, errors.New("不能除以零")
			}
			result = x / y
		}

		return mcp.NewToolResultText(fmt.Sprintf("%.2f", result)), nil
	})

	// 创建一个新的 SSEServer 实例，并传入 MCPServer
	sseServer := server.NewSSEServer(mcpServer)

	// 使用 SSEServer 处理当前请求
	sseServer.ServeHTTP(c.Writer, c.Request)
}
